<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function arr(cookies, k) {
      let ans = Infinity
      function backtrack(start, cur) {
        if (start >= cookies.length) {
          ans = Math.min(ans, Math.max(...cur))
          return
        }
        for (let i = 0; i < k; i++) {
          cur[i] += cookies[start]
          backtrack(start + 1, cur)
          cur[i] -= cookies[start]
        }
      }
      backtrack(0, new Array(k).fill(0))
      return ans
    };
    console.log(arr(cookies = [8, 15, 10, 20, 8], k = 2));
  </script>
</body>

</html>